package tools.fileup;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tools.util.RandomID;

public class UploadFile extends HttpServlet
{
  private static final long serialVersionUID = 56890894234786L;
  private String fileTypePathString = "";
  private String fileNameString = "";
  private UpUtil upUtil;
  Logger log = LoggerFactory.getLogger(UploadFile.class);

  public void doPost(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws ServletException, IOException
  {
    log.debug("进入……");
    paramHttpServletRequest.setCharacterEncoding("UTF-8");
    this.upUtil = new UpUtil(paramHttpServletRequest);
    boolean bool = ServletFileUpload.isMultipartContent(paramHttpServletRequest);
    if (bool == true)
      try
      {
        DiskFileItemFactory localDiskFileItemFactory = new DiskFileItemFactory();
        ServletFileUpload localServletFileUpload = new ServletFileUpload(localDiskFileItemFactory);
        List localList = localServletFileUpload.parseRequest(paramHttpServletRequest);
        Iterator localIterator = localList.iterator();
        getIsNotUpdateForm(localIterator);
        while (localIterator.hasNext())
        {
          FileItem localFileItem = (FileItem)localIterator.next();
          if (!localFileItem.isFormField())
          {
            String str = localFileItem.getName();
            if (str != null)
            {
              File localFile = new File(localFileItem.getName());
              if (localFile.exists())
              {
                localFileItem.write(createFile(str));
                log.debug("文件" + str + "上传成功");
              }
            }
          }
        }
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
      }
    else
      log.debug("the enctype must be multipart/form-data");
  }

  public void getIsNotUpdateForm(Iterator<FileItem> paramIterator)
  {
    while (paramIterator.hasNext())
    {
      FileItem localFileItem = (FileItem)paramIterator.next();
      if (localFileItem.isFormField())
      {
        String str = localFileItem.getFieldName();
        if (str.equals("fileSavePath"))
          this.fileTypePathString = localFileItem.getString();
        RandomID localRandomID = new RandomID();
        this.fileNameString = RandomID.GenTradeId();
      }
    }
  }

  public File createFile(String paramString)
  {
    File localFile = new File(this.upUtil.getSavePath(paramString), this.fileNameString + "." + paramString.substring(paramString.lastIndexOf(".") + 1));
    return localFile;
  }

  public void doGet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws ServletException, IOException
  {
    doPost(paramHttpServletRequest, paramHttpServletResponse);
  }
}

